package com.theme.passport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.passport.dao.SysMessageUserDao;
import com.theme.passport.entity.SysMessageUserEntity;
import com.theme.passport.entity.constant.MessageTypeEnum;
import com.theme.passport.entity.constant.MessageUserTypeEnum;
import com.theme.passport.service.SysMessageService;
import com.theme.passport.service.SysMessageUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service("sysMessageUserService")
public class SysMessageUserServiceImpl extends ServiceImpl<SysMessageUserDao, SysMessageUserEntity> implements SysMessageUserService {

    @Autowired
    private SysMessageService sysMessageService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String keyword = (String) params.get("key");
        String messageType = (String) params.get("messageType");
        Long userId = (Long) params.get("userId");
        IPage<SysMessageUserEntity> page = this.page(
                new Query<SysMessageUserEntity>().getPage(params),
                new QueryWrapper<SysMessageUserEntity>()
                        .like(StringUtils.isNotBlank(keyword), "username", keyword)
                        .eq(StringUtils.isNotBlank(messageType), "message_type", keyword)
                        .eq(userId != null, "to_user_id", userId)
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryUserNotice(Long userId, int toType, int messageType) {
        Map<String, Object> params = new HashMap<>();
        params.put("messageType", MessageTypeEnum.SYSTEM_NOTICE.getValue() +"");
        params.put("toType", MessageUserTypeEnum.PORTAL.getValue()+"");
        params.put("userId", userId+"");
        params.put("page", 1 + "");
        params.put("limit", 2 +"");
        IPage<SysMessageUserEntity> page = this.page(
                new Query<SysMessageUserEntity>().getPage(params),
                new QueryWrapper<SysMessageUserEntity>()
                        .eq("to_type", MessageUserTypeEnum.PORTAL.getValue())
                        .eq("message_type", MessageTypeEnum.SYSTEM_NOTICE.getValue())
                        .eq("to_user_id", userId)
                        .orderByDesc("create_time")
        );
        return new PageUtils(page);
    }

}